package com.qiezikaka.kaka.ui;

import android.app.Activity;


import com.qiezikaka.kaka.base.BasePresenter;
import com.qiezikaka.kaka.bean.AllBean;
import com.qiezikaka.kaka.bean.BannerInfo;
import com.qiezikaka.kaka.bean.FuZhuangInfo;
import com.qiezikaka.kaka.bean.HomeCateInfo;
import com.qiezikaka.kaka.bean.HotList;
import com.qiezikaka.kaka.bean.ShopInfo;
import com.qiezikaka.kaka.bean.UpdataVersionInfo;
import com.qiezikaka.kaka.net.DataManager;

import rx.Subscription;
import rx.functions.Action1;

public class MainPresenter extends BasePresenter<MainContract.View> implements MainContract.Presenter {

    public MainPresenter(Activity activity,MainContract.View view) {
        super(activity, view);

    }

    @Override
    public void getBannerList() {
        Subscription subscribe = DataManager.getInstance().getBannerList()
                .subscribe(new Action1<BannerInfo>() {
                    @Override
                    public void call(BannerInfo InfoData) {
                        if (InfoData != null) {
                            mView.onBannerList(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    @Override
    public void getHomeCate() {
        Subscription subscribe = DataManager.getInstance().getHomeCate()
                .subscribe(new Action1<HomeCateInfo>() {
                    @Override
                    public void call(HomeCateInfo InfoData) {
                        if (InfoData != null) {
                            mView.onHomeCate(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    /**
     * 服装列表
     * @param cid
     */
    @Override
    public void getFuZhuanList(String cid,String page,String limit,String hot) {
        Subscription subscribe = DataManager.getInstance().getFuZhuanList(cid,page,limit,hot)
                .subscribe(new Action1<FuZhuangInfo>() {
                    @Override
                    public void call(FuZhuangInfo InfoData) {
                        if (InfoData != null) {
                            mView.onFuZhuanList(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    @Override
    public void getFuZhuanHotList(String cid, String page, String limit, String hot) {
        Subscription subscribe = DataManager.getInstance().getFuZhuanList(cid,page,limit,hot)
                .subscribe(new Action1<FuZhuangInfo>() {
                    @Override
                    public void call(FuZhuangInfo InfoData) {
                        if (InfoData != null) {
                            mView.onFuZhuanHotList(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    /**
     * 版本更新
     * @param
     */
    @Override
    public void updateVersion() {
        Subscription subscribe = DataManager.getInstance().getUpdataVersion()
                .subscribe(new Action1<UpdataVersionInfo>() {
                    @Override
                    public void call(UpdataVersionInfo InfoData) {
                        if (InfoData != null) {
                            mView.onUpdataApp(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    /**
     * 获取商家信息
     * @param id
     */
    @Override
    public void getShopInfo(String id) {
        Subscription subscribe = DataManager.getInstance().getShopInfo(id)
                .subscribe(new Action1<ShopInfo>() {
                    @Override
                    public void call(ShopInfo InfoData) {
                        if (InfoData != null) {
                            mView.onGetShop(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    /**
     * 绑定商家
     * @param address
     * @param sid
     */
    @Override
    public void bindShop(String address, String sid) {
        Subscription subscribe = DataManager.getInstance().bindShop(address, sid)
                .subscribe(new Action1<AllBean>() {
                    @Override
                    public void call(AllBean InfoData) {
                        if (InfoData != null) {
                            mView.onBindShop(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    /**
     * 是否绑定设备
     * @param address
     */
    @Override
    public void isBind(String address) {
        Subscription subscribe = DataManager.getInstance().getIsBind(address)
                .subscribe(new Action1<AllBean>() {
                    @Override
                    public void call(AllBean InfoData) {
                        if (InfoData != null) {
                            mView.onIsBind(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }
}
